<?php

namespace App\Admin\Controllers\User;

use App\Models\CommonBank;
use App\Models\User;
use App\Models\UserBank;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BankController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户银行绑定信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserBank());

        $grid->column('id', ___('table.zy_user_bank.Id'));
        $grid->column('user_id', ___('table.zy_user_bank.User id'))->display(function ($id){
            $user = User::find($id);
            return $user?$user->name:'---';
        });
        $grid->column('type', ___('table.zy_user_bank.Type'))->display(function ($id){
            return UserBank::$type[$id];
        });
        $grid->column('account', ___('table.zy_user_bank.Account'));
        $grid->column('card', ___('table.zy_user_bank.Card'));
        $grid->column('bank_id', ___('table.zy_user_bank.Bank id'))->display(function ($id){
            $bank = CommonBank::find($id);
            return $bank?$bank->name:'---';
        });
        $grid->column('create_time', ___('table.zy_user_bank.Create time'));
        $grid->column('update_time', ___('table.zy_user_bank.Update time'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserBank::findOrFail($id));

        $show->field('id', ___('table.zy_user_bank.Id'));
        $show->field('user_id', ___('table.zy_user_bank.User id'))->as(function ($id){
            $user = User::find($id);
            return $user?$user->name:'---';
        });

        $show->field('type', ___('table.zy_user_bank.Type'))->as(function ($id){
            return UserBank::$type[$id];
        })->style('info');

        $show->field('account', ___('table.zy_user_bank.Account'));
        $show->field('card', ___('table.zy_user_bank.Card'));
        $show->field('bank_id', ___('table.zy_user_bank.Bank id'))->as(function ($id){
            $bank = CommonBank::find($id);
            return $bank?$bank->name:'---';
        });;
        //$show->field('create_time', ___('table.zy_user_bank.Create time'));
        //$show->field('update_time', ___('table.zy_user_bank.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserBank());

        $form->select('user_id', ___('table.zy_user_bank.User id'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user')->help("输入真实用户姓名");

        $form->radio('type', ___('table.zy_user_bank.Type'))->options(
           UserBank::$type

        )->default(1);
        $form->text('account', ___('table.zy_user_bank.Account'));
        $form->text('card', ___('table.zy_user_bank.Card'));
        $form->select('bank_id', ___('table.zy_user_bank.Bank id'))->options(function ($id) {
            $bank = CommonBank::find($id);
            if ($bank) {
                return [$bank->id => $bank->name];
            }
        })->ajax('/admin/ajax/banklist');

        //$form->number('create_time', ___('table.zy_user_bank.Create time'));
        //$form->number('update_time', ___('table.zy_user_bank.Update time'));

        return $form;
    }
}
